$(document).ready(init);

/**
 * 查询未通过审批的课题组长
 */
function init() {
	var queryParams = {
	    'employee.isCheck' : 0,
	    'employee.empType': 3
	};
	
	renderRegister(queryParams, false);
	
	$('#searchButton').click(searchByName);
}


function searchByName() {
	var queryParams = {
		'employee.empName' : $.trim($('#empName').val()),
		'employee.loginAccount' : $.trim($('#loginAccount').val()),
		'employee.isCheck' : 0,
	    'employee.empType': 3
	};
	
	renderRegister(queryParams, false);
}

/**
 * 初始化注册信息
 */
function renderRegister(queryParams, flag) {
		$('#reviewRegister').datagrid({
	    title:'',
	    iconCls:'icon-save',
	    singleSelect:true,
	    url:'iemployee-admin!queryEmployeeList.action',
	    idField: 'empID',
	    queryParams :queryParams,
	    columns:[[
	        {field:'empName',title:'真实姓名',width:fixWidth(0.08)},
	        {field:'loginAccount',title:'登录名',width:fixWidth(0.08)},
	        {field:'linkPhone',title:'联系电话',width:fixWidth(0.1)},
	        {field:'email',title:'邮箱',width:fixWidth(0.1)},
	        {field:'userType',title:'身份',width:fixWidth(0.1),
	        	formatter : function(value, rowData, rowIndex) {//0:院内、1:校内、2:校外
	        	    	var x = '<a href="#" onclick="showDetail('+ rowIndex + ',\'' + value +'\')">详情&nbsp;&nbsp;</a> ';
	        	    	
						if (value == '0') {
							return '院内';
				 		} else if (value == '1'){
				 			return '校内';
						} else if (value == '2') {
							return '校外';
						}
					}
	        },
	        {field:'deptID', title:'机构编号', hidden : true},
	        {field:'deptName', title:'机构',width:fixWidth(0.1), hidden : flag},
	        {field:'workEntity', title:'工作单位', width:fixWidth(0.1)},
	        {field:'empType', title:'用户类型', width:fixWidth(0.1),
	        	formatter:function(value, rowData, rowIndex) {
						var content ='';
						if ('1' == value) {
							content = '注册用户';
						} else if ('2' == value) {
							content = '正式用户';
						} else if ('3' == value) {
							content = '课题组长';
						} else if ('4' == value) {
							content = '仪器管理员';
						} else if ('5' == value) {
							content = '实验室管理员';
						} else if ('6' == value) {
							content = '系统管理员';
						} else {
							content = '';
						}
						
						return content;
					}
	        },
	        {field:'empID',title:'审核',width:fixWidth(0.1),align:'center',
	        	    formatter : function(value, rowData, rowIndex) {
	        	    	var x = '<a href="#" onclick="showDetail('+ rowIndex + ',\'' + value + '\')">详情&nbsp;&nbsp;</a> ';
	        	    	
						if (rowData.editing) {
							var e = '<span style="color:red">已审核</span> ';
							return x + e;
				 		} else {
							var s = '<a href="#" onclick="review('+ rowIndex + ',\'' + value +'\', \''  + rowData.deptID +  '\'' + ')">审核&nbsp;&nbsp;</a>';
							return x + s;
						}
					}
			}
	    ]],
	    onBeforeEdit:function(rowIndex,rowData){
			rowData.editing = true;
			$('#reviewRegister').datagrid('refreshRow', rowIndex);
		},
		onAfterEdit:function(rowIndex,rowData){
			rowData.editing = true;
			$('#reviewRegister').datagrid('refreshRow', rowIndex);
		},
	    pagination:true,
	    rownumbers:true,
	    fitColumns:true
	});
	
	
	$('#sure').click(closeDetail);
}

function review(index, value, deptID) 
{
	var options = {
		async : false,
   		type: 'POST',
   		url: 'iemployee!checkGroupRegister.action',
   		data: {
			'employee.isCheck' : 1,
			'employee.empType' : 3,
			'employee.deptID'  : deptID
		},
   		success : function(msg){
   			if ('null' == msg || '' == msg || null == msg) {
				 $.messager.confirm('审核','您确定审核通过?',function(r){
			        if (r){
						checkRepeat(index, value, deptID, 0);	
			        } else {
			        	$('#reviewRegister').datagrid('cancelEdit', index);
			        }
			     });
				
   			} else {
   				$.messager.confirm('确定', '已经存在课题组长：' + msg.empName + '，确定要覆盖？', function(r) {
   					 if (r){
						checkRepeat(index, value, deptID, 1);	
			        } else {
			        	$('#reviewRegister').datagrid('cancelEdit', index);
			        }
   				});
   			}
		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			$.messager.alert('确定', '处理失败：' + errorThrown, 'error')
		}
	}
	
	$.ajax(options);
}


function checkRepeat(index, value, deptID, exits) {
	var params = {
		'employee.isCheck' : 1,
		'employee.empID' : value,
		'employee.deptID'  : deptID,
		exits : exits
   }
	
	var option = {
   		type: 'POST',
   		url: 'iemployee-admin!reviewGroupRegister.action',
   		data: params,
   		success : function(msg){
   			if (msg == 'success') {
   				$.messager.alert('确定', '恭喜您审批成功', 'info')
	     	
	     		$('#reviewRegister').datagrid('beginEdit', index);	
   			} else {			   				
   				$.messager.alert('确定', '处理失败，请稍后尝试', 'error')
   			}
		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			$.messager.alert('确定', '处理失败：' + errorThrown, 'error');
		}
	}
	
	$.ajax(option); 
}

function showDetail(index, value) {
	var k = $('#detail label');
	
	k.each(function(index, domEle) {
		$(domEle).text('');
	});
	
	var params = {
		'employee.empID' : value
	};
	
	$.getJSON("iemployee!getEmployeeDetail.action", params, function(json) {
		for(p in json) {
			k.each(function(index, domEle) {
				if($(domEle).attr('id') == p ) {
					var content = '';
					if (p == 'userType') {// 身份
						if ('0' == json[p]) {
							content = '院内';
						} else if ('1' == json[p]) {
							content = '校内';
						} else {
							content = '校外';
						}
					} else if (p == 'empType') {
						if ('1' == json[p]) {
							content = '注册用户';
						} else if ('2' == json[p]) {
							content = '正式用户';
						} else if ('3' == json[p]) {
							content = '课题组长';
						} else if ('4' == json[p]) {
							content = '仪器管理员';
						} else if ('5' == json[p]) {
							content = '实验室管理员';
						} else if ('6' == json[p]) {
							content = '系统管理员';
						} else {
							content = '';
						}
					} else if (p == 'education') {//0:文盲、1:小学、2:初中、3:高中、4:大专、5:本科、6:硕士、7:博士、8:博士后
						if ('0' == json[p]) {
							content = '文盲';
						}
						else if ('1' == json[p]) {
							content = '小学';
						} else if ('2' == json[p]) {
							content = '初中';
						} else if ('3' == json[p]) {
							content = '高中';
						} else if ('4' == json[p]) {
							content = '大专';
						} else if ('5' == json[p]) {
							content = '本科';
						} else if ('6' == json[p]) {
							content = '硕士';
						} else if ('7' == json[p]) {
							content = '博士';
						} else if ('8' == json[p]) {
							content = '博士后';
						} 
						else {
							content = '未知';
						}
					} else {
						content = json[p];
					}
					
					$(domEle).text(content);
				}
			});			
		}
    });
    
    $('#detail').window({
    	collapsible:false,
    	minimizable:false,
    	maximizable:false
	});
}

function closeDetail() {
	$('#detail').window('close');
}